﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;

namespace UseEntityJig
{
    public class DrawLineDynamicLengthTextJig
    {
        /// <summary>
        /// 画线 在直线右上方（鼠标右上方）实时显示长度文本
        /// </summary>
        [CommandMethod("LineDynamicLength")]
        public void LineDynamicLength()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptPointResult pprStart = ed.GetPoint("Select start point: ");
            if (pprStart.Status != PromptStatus.OK)
                return;

            Point3d startPoint = pprStart.Value;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                LineJig jig = new LineJig(startPoint);

                // 开始拖动操作
                if (ed.Drag(jig).Status == PromptStatus.OK)
                {
                    jig.AppendEntities(btr, tr);
                    tr.Commit();
                }
            }
        }
    }

    public class LineJig : DrawJig
    {
        private Point3d _startPoint;
        private Point3d _endPoint;
        private Line _line;
        private DBText _lengthText;

        public LineJig(Point3d startPoint)
        {
            _startPoint = startPoint;
            _endPoint = startPoint;

            // 初始化线条实体
            _line = new Line
            {
                StartPoint = _startPoint,
                EndPoint = _endPoint
            };

            // 初始化文本实体
            _lengthText = new DBText
            {
                Height = 2.5,
                Position = _endPoint,
                TextString = "0",
                Layer = "0",
                ColorIndex = 1
            };
        }

        protected override bool WorldDraw(WorldDraw draw)
        {
            // 绘制实时更新的直线
            draw.Geometry.Draw(_line);

            // 绘制实时更新的长度文本
            draw.Geometry.Draw(_lengthText);

            return true;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions("Select end point: ")
            {
                BasePoint = _startPoint,
                UseBasePoint = true
            };

            PromptPointResult res = prompts.AcquirePoint(jigOpts);

            if (res.Status == PromptStatus.OK)
            {
                if (res.Value.IsEqualTo(_endPoint))
                    return SamplerStatus.NoChange;

                _endPoint = res.Value;
                UpdateEntities();
                return SamplerStatus.OK;
            }

            return SamplerStatus.Cancel;
        }

        private void UpdateEntities()
        {
            // 更新线条的终点
            _line.EndPoint = _endPoint;

            // 更新长度文本的内容和位置
            double length = _line.Length;
            _lengthText.TextString = $"{length:F0}";
            _lengthText.Position = _endPoint.Add(new Vector3d(1, 1, 0));
        }

        public void AppendEntities(BlockTableRecord btr, Transaction tr)
        {
            // 将线和文本都添加到模型空间
            btr.AppendEntity(_line);
            tr.AddNewlyCreatedDBObject(_line, true);
            btr.AppendEntity(_lengthText);
            tr.AddNewlyCreatedDBObject(_lengthText, true);
        }
    }

}



